//
//  HRSRequestNetWorking.m
//  HRSNetWorking
//
//  Created by huangrensheng on 16/4/18.
//  Copyright © 2016年 laber. All rights reserved.
//

#import "HRSRequestNetWorking.h"

@implementation HRSRequestNetWorking

#pragma GET
/**
 *  get请求方式
 *
 *  @param url      请求的地址
 *  @param params   参数
 *  @param callBack 回掉函数
 */
- (void)getWithUrl:(NSString *)url params:(NSDictionary *)params callBack:(callBack)callBack{
    if (params != nil) {
        url= [[self getFormartUrlWithParams:params url:url] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    }
    NSURL *reUrl = [NSURL URLWithString:url];
    
    NSLog(@"URL %@",reUrl);
    NSURLRequest *request = [NSURLRequest requestWithURL:reUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task =[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        callBack(data,response,error);
    }];
    [task resume];
}

/**
 *  get请求 地址拼接   get请求参数直接拼接在地址后面
 *
 *  @param params  请求的参数
 *  @param baseUrl 请求的地址
 *
 *  @return 返回拼接好的地址
 */
- (NSString *)getFormartUrlWithParams:(NSDictionary *)params url:(NSString *)baseUrl{
    NSMutableString *url = [NSMutableString stringWithString:baseUrl];
    for (int i = 0; i<params.count ; i++){
        NSString *key = [params allKeys][i];
        if (i == 0) {
            [url appendFormat:@"?%@=%@",key,params[key]];
        }else{
            if ([params[key] isEqualToString:@""]) {
                continue;
            }
            [url appendFormat:@"&%@=%@",key,params[key]];
        }
    }
    return url;
}

#pragma POST
/**
 *  post 请求地址
 *
 *  @param url      请求的地址
 *  @param params   请求的参数
 *  @param callBack 回调函数
 */
- (void)postWithUrl:(NSString *)url params:(NSDictionary *)params callBack:(callBack)callBack{
    NSURLSession *session = [NSURLSession sharedSession];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    request.HTTPMethod = @"POST";
    if (params) {
        request.HTTPBody = [[self postFormartUrlWithParams:params] dataUsingEncoding:NSUTF8StringEncoding];
    }
    NSURLSessionDataTask *task =[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        callBack(data,response,error);
    }];
    [task resume];
}

/**
 *  拼接post 的请求体
 *  @param params 参数
 *  @return 反悔拼接好的请求体
 */
- (NSString *)postFormartUrlWithParams:(NSDictionary *)params{
     NSMutableString *body = [NSMutableString stringWithString:@""];
    for (int i = 0; i<params.count ; i++){
        NSString *key = [params allKeys][i];
        if (i == 0) {
            [body appendFormat:@"%@=%@",key,params[key]];
        }else{
            if ([params[key] isEqualToString:@""]) {
                continue;
            }
            [body appendFormat:@"&%@=%@",key,params[key]];
        }
    }
    return body;
}
@end
